package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import com.itheima.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class StudentController {

    //todo 因为springMVC和spring没有整合，这个位置无法进行注入
    @Autowired
    private StudentService studentService;

    @GetMapping("/student")
    public ResultInfo findAll() {
        List<Student> studentList=studentService.findAll();
       // ArrayList<Student> arrayList = new ArrayList<Student>();
        //arrayList.add(new Student("00001","张三",new Date(),"顺义石门"));

        return ResultInfo.success(studentList);
    }

    @PostMapping("/student")
    public ResultInfo save(@RequestBody Student student) {
        studentService.save(student);
        return ResultInfo.success(null);
    }

    @GetMapping("/student/{number}")
    public ResultInfo findByNumber(@PathVariable("number") String number) {
        Student student=studentService.findByNumber(number);
        return ResultInfo.success(student);
    }

    @PutMapping("/student")
    public ResultInfo update(@RequestBody Student student) {
        studentService.update(student);
        return ResultInfo.success(null);
    }

    @DeleteMapping("/student/{number}")
    public ResultInfo deleteByNumber(@PathVariable("number") String number) {
        studentService.deleteByNumber(number);
        return ResultInfo.success(number);
    }
}
